/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示 GTIM1 的 CH4(PB03) 管脚驱动无源蜂鸣器
  *          请在 PB03 管脚连接无源蜂鸣器
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：短接J22
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_gtim.h"
#include "sym32l010_Startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
GTIM_HandleTypeDef hgtim1;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void GTIM1_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);

    // 初始化GTIM1
    GTIM1_Init();

    // 请在PB03 管脚连接无源蜂鸣器
    // 每两秒发出一次“嘀-嘀”
    while (1)
    {
        GTIM_CCxChannelCmd(hgtim1.Instance, GTIM_CHANNEL_4, GTIM_CCx_ENABLE); // Beep On  - 函数
        HAL_Delay(75);
        GTIM_CCxChannelCmd(hgtim1.Instance, GTIM_CHANNEL_4, GTIM_CCx_DISABLE); // Beep Off - 函数
        HAL_Delay(50);

        SYM_GTIM1->CCER_f.CC4E = 1;                                   // Beep On  - 寄存器
        HAL_Delay(75);
        SYM_GTIM1->CCER_f.CC4E = 0;                                   // Beep Off - 寄存器
        HAL_Delay(1800);
    }
}


/*******************************************************************************
 * @brief  GTIM1初始化
 * @param  None
 * @retval None
 */
static void GTIM1_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));

    HAL_SYSCTRL_GPIOB_CLK_ENABLE();
    HAL_SYSCTRL_GTIM1_CLK_ENABLE();

    //------------------------------------------------------------
    // 配置 GTIM1_CH4 端口
    AFx_GTIM1CH4_PB03();
    GPIO_InitStruct.Pins = GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOB, &GPIO_InitStruct);

    //------------------------------------------------------------
    // 配置GTIM1
    MemClr(&hgtim1, sizeof(hgtim1));

    hgtim1.Instance           = SYM_GTIM1;
    hgtim1.Init.ClockDivision = GTIM_CLOCKDIVISION_DIV1;
    hgtim1.Init.CounterMode   = GTIM_COUNTERMODE_UP;
    hgtim1.Init.Prescaler     = 1 - 1;
    hgtim1.Init.Period        = 8888 - 1;                             // 溢出频率为5.4KHz (48MHz / 8888 = 5.4KHz)
    hgtim1.Init.AutoReloadPreload = GTIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_GTIM_Base_Init(&hgtim1);

    hgtim1.OcInit.OCMode      = GTIM_OCMODE_TOGGLE;
    hgtim1.OcInit.Pulse       = 8888 - 1;
    hgtim1.OcInit.OCPolarity  = GTIM_OCPOLARITY_HIGH;
    hgtim1.OcInit.OCFastMode  = GTIM_OCFAST_DISABLE;
    HAL_GTIM_OC_ConfigChannel(&hgtim1, &hgtim1.OcInit, GTIM_CHANNEL_4);

    // 启动GTIM1
    HAL_GTIM_Base_Start(&hgtim1);
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
